/* internal/js/order/form.js */

function resetFields () {
	document.getElementById('orderID').value = '';
	document.getElementById('orderItemID').value = '';
	document.getElementById('menuItemID').value = '';
	document.getElementById('firstname').value = '';
	document.getElementById('lastname').value = '';
	document.getElementById('phone').value = '';
	document.getElementById('createdBy').innerHTML = '';
	document.getElementById('datePlaced').innerHTML = '';
	document.getElementById('orderStatus').selectedIndex = '0';
	document.getElementById('paymentStatus').selectedIndex = '0';
	document.getElementById('orderType').selectedIndex = '0';
	document.getElementById('delivery').innerHTML = '';
	document.getElementById('orderItemList').innerHTML = '';
	document.getElementById('itemNumber').value = '';
	document.getElementById('itemName').value = '';
	
	hideFieldMessage('firstnameMessage');
	hideFieldMessage('lastnameMessage');
	hideFieldMessage('phoneMessage');
	hideFieldMessage('orderStatusMessage');
	hideFieldMessage('paymentStatusMessage');
	hideFieldMessage('itemNumberMessage');
	
	selectResultRow(0);
	selectOrderItemRow(0);
	selectMenuItemRow(0);
}

function loadPage () {
	listOrderStatus();
	listPaymentStatus();
	listMenuItem();
}


/* To build order result table */

function startResultsTable () {
	var html = '<table class="searchResults">' +
	           '<tr>' +
	           '<th> Name </th>' +
	           '<th> Placed Date </th>' +
	           '<th> Placed Time </th>' +
	           '<th> Order Status </th>' +
	           '</tr>';
	return html;
}


function buildTable (orders) {
	var html = startResultsTable();
	var count = orders.childNodes.length;
	var ids = new Array(count);
	var showOptionAll = document.showOpForm.showOption[0].checked;
	var counter = 0;
	for (var loop = 0; loop < count; loop++) {
		var order = orders.childNodes[loop];
		var id = order.getElementsByTagName('id')[0].childNodes[0].nodeValue;
		var status;
		if (order.getElementsByTagName('status')[0].childNodes.length == 0)
			status = '';
		else
			status = order.getElementsByTagName('status')[0].childNodes[0].nodeValue;
		
		if (showOptionAll || status != 'Done') {
			// Get all the information from order xml
			ids[counter++] = id;	
			var patron;
			if (order.getElementsByTagName('patron')[0].childNodes.length > 0) {
				patron = order.getElementsByTagName('patron')[0];
				if (patron != null) {
					var firstname = patron.getElementsByTagName('firstname')[0].childNodes[0].nodeValue;
				
					var lastname;
					if (patron.getElementsByTagName('lastname')[0].childNodes.length == 0)
						lastname = '';
					else
						lastname = patron.getElementsByTagName('lastname')[0].childNodes[0].nodeValue;
			
					var phone;
					if (patron.getElementsByTagName('phone')[0].childNodes.length == 0)
						phone = '';
					else
						phone = patron.getElementsByTagName('phone')[0].childNodes[0].nodeValue;
				}
				
			}	
			if (order.getElementsByTagName('createdby')[0].childNodes.length != 0) {
				var createdbyNode = order.getElementsByTagName('createdby')[0].childNodes[0];
				if (createdbyNode != null) {
					var empFirstname = createdbyNode.getElementsByTagName('firstname')[0].childNodes[0].nodeValue;
					var empLastname = createdbyNode.getElementsByTagName('lastname')[0].childNodes[0].nodeValue;
					var createdBy = empFirstname + ' '+ empLastname;
				}
			}
			else 
				createdBy = firstname + ' ' + lastname;
				
			if (order.getElementsByTagName('placed')[0].childNodes.length != 0) {
				var placed = order.getElementsByTagName('placed')[0];
				var day;
				if (placed.getElementsByTagName('day')[0].childNodes.length == 0)
					day = '';
				else
					day = placed.getElementsByTagName('day')[0].childNodes[0].nodeValue;
				
				
				var month;
				if (placed.getElementsByTagName('month')[0].childNodes.length == 0)
					month = '';
				else
					month = placed.getElementsByTagName('month')[0].childNodes[0].nodeValue;
				
				var hour = placed.getElementsByTagName('hour')[0].childNodes[0].nodeValue;
				var minute = placed.getElementsByTagName('minute')[0].childNodes[0].nodeValue;
			
				var year;
				if (placed.getElementsByTagName('year')[0].childNodes.length == 0)
					year = '';
				else
					year = order.getElementsByTagName('year')[0].childNodes[0].nodeValue;
			
			}
			
			var paymentStatus;
			if (order.getElementsByTagName('paymentstatus')[0].childNodes.length == 0)
				paymentStatus = '';
			else
				paymentStatus = order.getElementsByTagName('paymentstatus')[0].childNodes[0].nodeValue;
			
			var deliveryID;
			if (order.getElementsByTagName('delivery')[0].childNodes.length == 0)
				deliveryID = 0;
			else
				deliveryID = order.getElementsByTagName('delivery')[0]
				                   .getElementsByTagName('id')[0].childNodes[0].nodeValue;

			var subtotal = order.getElementsByTagName('subtotal')[0].childNodes[0].nodeValue;
			var tax = order.getElementsByTagName('tax')[0].childNodes[0].nodeValue;
			var orderTotal = order.getElementsByTagName('order-total')[0].childNodes[0].nodeValue;
			
			if (order.getElementsByTagName('balancepaid')[0].childNodes.length == 0)
				var balancePaid = order.getElementsByTagName('balancepaid')[0].childNodes[0].nodeValue;
			else
				balancePaid = 0.00;
			
			html += addResultRow(id, firstname, lastname, phone, createdBy, day,
					month, year, hour, minute, status, paymentStatus, deliveryID, subtotal, tax, orderTotal, balancePaid);
		
		}
	}
	// shrink the array
	var newIds = new Array(counter);
	for (var i = 0; i < counter; i++)
		newIds[i] = ids[i];
	
	html += endResultsTable(newIds);
	return html;
}

function addResultRow (id, firstname, lastname, phone, createdBy, day,
			month, year,  hour, minute, status, paymentStatus, deliveryID, subtotal, tax, orderTotal, balancePaid) {
	var	html =
		'<form id="listOrderForm' + id + '">' +
		'<input id="firstname" value="' + firstname + '" type="hidden" />' +
		'<input id="lastname" value="' + lastname + '" type="hidden" />' +
		'<input id="phone" value="' + phone + '" type="hidden" />' +
		'<input id="createdBy" value="' + createdBy + '" type="hidden" />' +
		'<input id="day" value="' + day + '" type="hidden" />' +
		'<input id="month" value="' + month + '" type="hidden" />' +
		'<input id="year" value="' + year + '" type="hidden" />' +
		'<input id="hour" value="' + hour + '" type="hidden" />' +
		'<input id="minute" value="' + minute + '" type="hidden" />' +
		'<input id="status" value="' + status + '" type="hidden" />' +
		'<input id="paymentStatus" value="' + paymentStatus + '" type="hidden" />' +
		'<input id="deliveryID" value="' + deliveryID + '" type="hidden" />' +
		'<input id="subtotal" value="' + subtotal + '" type="hidden" />' +
		'<input id="tax" value="' + tax + '" type="hidden" />' +
		'<input id="orderTotal" value="' + orderTotal + '" type="hidden" />' +
		'<input id="balancePaid" value="' + balancePaid + '" type="hidden" />' +
		'</form>' +
		'<tr id="listOrderRow' + id + '" class="searchResultsRow" onclick="showResult(\'' + id + '\'), listOrderItem(\'' + id + '\')">' +
		'<td>' + firstname + " "+ lastname  + '</td>' +
		'<td class="alignCenter">' + convertMonth(month) +' '+ day + ', ' + year + '</td>' +
		'<td class="alignCenter">' + hour + ':' + minute + '</td>' +
		'<td class="alignCenter">' + status + '</td>';
	return html;
}

function endResultsTable (ids) {
	var html =
		'<input id="resultsIds" value="' + ids.join() +'" type="hidden" />' + 
		'</table>';
	return html;
}


function selectResultRow (id) {
	if (document.getElementById('resultsIds') != null) {
		var ids = document.getElementById('resultsIds').value.split(',');
		var count = ids.length;
		for (var loop = 0; loop < count; loop++) {
			document.getElementById('listOrderRow' + ids[loop]).className = 'searchResultsRow';
		}
		try {
			document.getElementById('listOrderRow' + id).className = 'selectedResultsRow';
		} catch (e) {
		}
	}
}

function showResult (id) {
	selectResultRow(id);
	var frm = document.getElementById('listOrderForm' + id);
	
	document.getElementById('orderID').value = id;
	document.getElementById('firstname').value = frm.firstname.value;
	document.getElementById('lastname').value = frm.lastname.value;
	document.getElementById('phone').value = frm.phone.value;
	document.getElementById('createdBy').innerHTML = frm.createdBy.value;
	
	// set the Date/Time Placed field
	var dayPlaced = frm.day.value;
	var monthPlaced = frm.month.value;
	var yearPlaced = frm.year.value;
	var hour = frm.hour.value;
	var minute = frm.minute.value;
	
	document.getElementById('datePlaced').innerHTML = '<font size="3">' + dayPlaced +
		 ', ' + convertMonth(monthPlaced)+' ' + yearPlaced +'   ' + hour + ' : ' + minute + '</font>';
	
	// set the order status and payment status drop down menu
	var orderStatus = frm.status.value;
	var statusList = document.getElementById('orderStatus');
	for (var i = 0; i < statusList.length; i++){
		if (statusList.options[i].value == orderStatus){
			statusList.selectedIndex = i;
			break;
		}
	}
	
	var paymentStatus = frm.paymentStatus.value;
	statusList = document.getElementById('paymentStatus');
	for (var i = 0; i < statusList.length; i++){
		if (statusList.options[i].value == paymentStatus){
			statusList.selectedIndex = i;
			break;
		}
	}
	
	var deliveryID = frm.deliveryID.value;
	if (deliveryID > 0) {
		document.getElementById('orderType').selectedIndex = '1';
		document.getElementById('delivery').innerHTML = 
					'<form action="../delivery/index.jsp" method="post">' +
					'<input type="submit" value="Manage the delivery" />' +
					'<input type="hidden" name="deliveryID" value="' + deliveryID + '" />' +
					'</form>';
	}
	else {
		document.getElementById('orderType').selectedIndex = '0';
		document.getElementById('delivery').innerHTML = 'Pickup Order';
	}
	
	//clearResults();
}

/* Create Order Item table */

function buildOrderItemTable(orderitems, orderID) {
	var html = '<table class="searchResults">' +
	    '<tr>' +
	    '<th> Item No. </th>' +
	    '<th> Name </th>' +
	    '<th> Quantity </th>' +
	    '<th> Item Price </th>' +
	    '<th> Total Price </th>' +
	    '</tr>';
	var count = orderitems.childNodes.length;
	var ids = new Array(count);
	for (var loop = 0; loop < count; loop++) {
		var orderitem = orderitems.childNodes[loop];
		var id = orderitem.getElementsByTagName('id')[0].childNodes[0].nodeValue;
		ids[loop] = id;
		
		// Get all the information from orderitem xml
		var name;
		if (orderitem.getElementsByTagName('name')[0].childNodes.length > 0)
			name = orderitem.getElementsByTagName('name')[0].childNodes[0].nodeValue;
		else
			name = '';
		
		var itemNo;
		if (orderitem.getElementsByTagName('number')[0].childNodes.length > 0)
			itemNo = orderitem.getElementsByTagName('number')[0].childNodes[0].nodeValue;
		else
			itemNo = '';
		
		var price;
		if (orderitem.getElementsByTagName('price')[0].childNodes.length > 0)
			price = orderitem.getElementsByTagName('price')[0].childNodes[0].nodeValue;
		else
			price = '';

		var quantity;
		if (orderitem.getElementsByTagName('quantity')[0].childNodes.length > 0)
			quantity = orderitem.getElementsByTagName('quantity')[0].childNodes[0].nodeValue;
		else
			quantity = '';
		
		var total;
		if (orderitem.getElementsByTagName('total')[0].childNodes.length > 0)
			total = orderitem.getElementsByTagName('total')[0].childNodes[0].nodeValue;
		else
			total = '';
		
		html += addOrderItemRow(id, name, itemNo, price, quantity, total);
	}
	
	// get the subtotal, tax and total of the order
	var frm = document.getElementById('listOrderForm' + orderID);
	var subtotal = frm.subtotal.value;
	var tax = frm.tax.value;
	var orderTotal = frm.orderTotal.value;
	var balancePaid = frm.balancePaid.value;
	html +=
		'<tr></tr>' +
		'<tr><td colspan="4" align="right"><b>Subtotal:</b></td>' + '<td align="right"><b>$' + parseFloat(subtotal).toFixed(2) + '</b></td></tr>' +
		'<tr><td colspan="4" align="right"><b>Tax:</b></td>' + '<td align="right"><b>$' + parseFloat(tax).toFixed(2) + '</b></td></tr>' + 
		'<tr><td colspan="4" align="right"><b>Total:</b></td>' + '<td align="right"><b>$' + parseFloat(orderTotal).toFixed(2) + '</b></td></tr>' +
		'<tr><td colspan="4" align="right"><b>Balance Paid:' +  '<td align="right"><b>$' + parseFloat(balancePaid).toFixed(2) + '</b></td></tr>' +
		'<tr><td colspan="5"><hr /></td></tr>' +
		'<tr><td colspan="4" align="right"><b>Outstanding Balance:' +  '<td align="right"><b>$' + (parseFloat(orderTotal) - parseFloat(balancePaid)).toFixed(2) + '</b></td></tr>' +
		'<input id="orderItemIds" value="' + ids.join() +'" type="hidden" />' + 
		'</table>';
	return html;
}

function addOrderItemRow(id, name, itemNo, price, quantity, total) {
	var	html =
		'<form id="listOrderItemForm' + id + '">' +
		'<input id="name" value="' + name + '" type="hidden" />' +
		'<input id="itemNo" value="' + itemNo + '" type="hidden" />' +
		'<input id="price" value="' + price + '" type="hidden" />' +
		'<input id="quantity" value="' + quantity + '" type="hidden" />' +
		'</form>' +
		'<tr id="listOrderItemRow' + id + '" class="searchResultsRow" onclick="setOrderItemId(\'' + id + '\'), selectOrderItemRow(\'' + id + '\')">' +
		'<td align="right">' + itemNo  + '</td>' +
		'<td align="center">' + name + '</td>' +
		'<td align="right">' +quantity + '</td>' +
		'<td align="right">' + '$'+ parseFloat(price).toFixed(2) + '</td>' + 
		'<td align="right">' + '$'+ parseFloat(total).toFixed(2) + '</td>' + 
		'</tr>';
	return html;
}

function selectOrderItemRow (id) {
	if (document.getElementById('orderItemIds') != null) {
		var ids = document.getElementById('orderItemIds').value.split(',');
		var count = ids.length;
		for (var loop = 0; loop < count; loop++) {
			document.getElementById('listOrderItemRow' + ids[loop]).className = 'searchResultsRow';
		}
		try {
			document.getElementById('listOrderItemRow' + id).className = 'selectedResultsRow';
		} catch (e) {
		}
	}
}

function setOrderItemId(id) {
	document.getElementById('orderItemID').value = id;
}


/* Create Menu Item Table */

function buildMenuItemTable(menuitems) {
	var html = '<table class="searchResults">' +
	    '<tr>' +
	    '<th> Item No. </th>' +
	    '<th> Name </th>' +
	    '<th> Price </th>' +
	    '</tr>';
	var count = menuitems.childNodes.length;
	var ids = new Array(count);
	for (var loop = 0; loop < count; loop++) {
		var menuitem = menuitems.childNodes[loop];
		var id = menuitem.getElementsByTagName('id')[0].childNodes[0].nodeValue;
		ids[loop] = id;
		
		// Get all the information from menuitem xml
		var name;
		if (menuitem.getElementsByTagName('menu-item-name')[0].childNodes.length > 0)
			name = menuitem.getElementsByTagName('menu-item-name')[0].childNodes[0].nodeValue;
		else
			name = '';
		
		var itemNo;
		if (menuitem.getElementsByTagName('number')[0].childNodes.length > 0)
			itemNo = menuitem.getElementsByTagName('number')[0].childNodes[0].nodeValue;
		else
			itemNo = '';
		
		var price;
		if (menuitem.getElementsByTagName('price')[0].childNodes.length > 0)
			price = menuitem.getElementsByTagName('price')[0].childNodes[0].nodeValue;
		else
			price = '';
		
		html += addMenuItemRow(id, name, itemNo, price);
	}
	html +=
		'<input id="menuItemIds" value="' + ids.join() +'" type="hidden" />' + 
		'</table>';
	return html;
}

function addMenuItemRow(id, name, itemNo, price) {
	var	html =
		'<form id="listMenuItemForm' + id + '">' +
		'<input id="name" value="' + name + '" type="hidden" />' +
		'<input id="itemNo" value="' + itemNo + '" type="hidden" />' +
		'<input id="price" value="' + price + '" type="hidden" />' +
		'</form>' +
		'<tr id="listMenuItemRow' + id + '" class="searchResultsRow" onclick="setMenuItemId(\'' + id + '\'), selectMenuItemRow(\'' + id + '\')">' +
		'<td align="right">' + itemNo  + '</td>' +
		'<td align="center">' + name + '</td>' +
		'<td align="right">' + '$'+ parseFloat(price).toFixed(2) + '</td></tr>';
	return html;
}

function selectMenuItemRow (id) {
	var ids = document.getElementById('menuItemIds').value.split(',');
	var count = ids.length;
	for (var loop = 0; loop < count; loop++) {
		document.getElementById('listMenuItemRow' + ids[loop]).className = 'searchResultsRow';
	}
	try {
		document.getElementById('listMenuItemRow' + id).className = 'selectedResultsRow';
	} catch (e) {
	}
}

function setMenuItemId(id) {
	document.getElementById('menuItemID').value = id;
}




function convertMonth(monthInt) {
	var month = null;
	switch (monthInt)
	{
	case '0': month = "January"; break;
	case '1': month = "February"; break;
	case '2': month = "March"; break;
	case '3': month = "April"; break;
	case '4': month = "May"; break;
	case '5': month = "June"; break;
	case '6': month = "July"; break;
	case '7': month = "August"; break;
	case '8': month = "September"; break;
	case '9': month = "October"; break;
	case '10': month = "November"; break;
	case '11': month = "December"; break;
	}
	return month;
}